package gui;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import commands.AddEmployee;
import containers.EmployeeContainer;
import entities.Employee;

/**
 * 
 * @author Samuel Ferguson sff803
 * 
 */
public class ManagerCreateEmployeePanel extends JPanel {
	ManagerCreateEmployeePanel thisone;
	ValueEntryPanel enterPW;
	int id;
	Object[][] data;

	public ManagerCreateEmployeePanel() {
		super(new GridLayout(2, 0));
		thisone = this;
		JButton button;
		String[] columnNames = { "id", "password" };
		data = new Object[EmployeeContainer.dictionary().size()][columnNames.length];

		Employee temp;
		id = 0;
		int i = 0;
		Iterator<Employee> iter = EmployeeContainer.dictionary().values()
				.iterator();
		while (iter.hasNext()) {
			temp = iter.next();
			if (temp.getId() > id)
				id = temp.getId();
			data[i][0] = temp.getId();
			data[i][1] = temp.getPassword();
			i++;
		}
		id++;
		JTable table = new JTable(data, columnNames);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);

		// Create the scroll pane and add the table to it.
		JScrollPane scrollPane = new JScrollPane(table);

		// Add the scroll pane to this panel.
		add(scrollPane);

		String myString = "The next EmployeeID will be " + id;
		add(new JLabel(myString));
		enterPW = new ValueEntryPanel("Enter password");
		add(enterPW);

		JPanel okay = new JPanel();
		button = new JButton("Create Employee");
		button.addActionListener(new MakeEmployee());
		okay.add(Box.createVerticalGlue());
		okay.add(button);

		add(okay);

	}

	private class MakeEmployee implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent event) {

			String myString = enterPW.getValueAsString();
			if (!myString.equals("")) {
				Employee newguy = new Employee(id, myString);
				new AddEmployee(newguy);
			}
			JPanel maAndOrPa = (JPanel) thisone.getParent();
			maAndOrPa.remove(thisone);
			maAndOrPa.add(new ManagerCreateEmployeePanel());
			maAndOrPa.revalidate();
		}
	}

	public static final long serialVersionUID = 1;

}
